package mycoffee.order.xmlhandler;

import java.util.ArrayList;

import mycoffee.order.model.Area;
import mycoffee.order.model.Table;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.text.TextUtils;

public class AreasHandler extends DefaultHandler {
	/** list of categories. */
	private ArrayList<Area> mAreas;
	private Area mCurrentArea = null;

	public AreasHandler() {
		mAreas = new ArrayList<Area>();
	}

	public ArrayList<Area> getResult() {
		return mAreas;
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		try {
			if (TextUtils.equals(localName, XmlConstants.AREA)) {
				this.mCurrentArea = new Area();
				this.mAreas.add(this.mCurrentArea);
				mCurrentArea.setId(Integer.parseInt(attributes.getValue(XmlConstants.ID)));
				mCurrentArea.setName(attributes.getValue(XmlConstants.NAME));
				mCurrentArea.setName_vi(attributes.getValue(XmlConstants.NAME_VN));
				mCurrentArea.setName_en(attributes.getValue(XmlConstants.NAME_EN));
				mCurrentArea.setName_de(attributes.getValue(XmlConstants.NAME_DE));
			} else if (TextUtils.equals(localName, XmlConstants.TABLE)) {
				if (mCurrentArea == null) {
					return;
				}
				
				// create new table.
				Table table = new Table();
				ArrayList<Table> tables = mCurrentArea.getTables();
				if (tables != null) {
					tables.add(table);
					// add info.
					table.setId(Integer.parseInt(attributes.getValue(XmlConstants.ID)));
					table.setName(attributes.getValue(XmlConstants.NAME));
					table.setName_vi(attributes.getValue(XmlConstants.NAME_VN));
					table.setName_en(attributes.getValue(XmlConstants.NAME_EN));
					table.setName_de(attributes.getValue(XmlConstants.NAME_DE));
					table.setAreaID(this.mCurrentArea.getId());
				}
			}
		} catch (Exception e) {
		}

	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		super.endElement(uri, localName, qName);
	}

}
